Jyro Python Robot Simulator¶
Pure-Python simulator for robots.¶
In [2]:
from jyro.simulator import *
import math
In [3]:
sim = Physics()
In [4]:
def world(sim):
sim.addBox(0, 0, 5, 5, fill="backgroundgreen", wallcolor="lightgrey") # meters
sim.addBox(1, 1, 2, 2, "purple")
## brightness of 1 is radius 1 meter
sim.addLight(4, 4, 1.00, color=Color(255, 255, 0, 64))
sim.addLight(4, 2, 1.00, color=Color(255, 255, 0, 64))
In [5]:
world(sim)
In [6]:
import random
class MyPioneer(Pioneer):
def __init__(self, name, x, y, angle):
Pioneer.__init__(self, name, x, y, angle)
self.addDevice(PioneerFrontSonars(maxRange=5.0))
#self.addDevice(Gripper())
self.addDevice(PioneerFrontLightSensors(5))
def brain(self):
self.move(random.random() * 2 - 1,
random.random() * 2 - 1)
In [7]:
robot = MyPioneer("Pioneer", 2.50, 4.50, math.pi / 2) # meters, radians
In [8]:
robot
Out[8]:
In [9]:
sim.addRobot(robot)
In [10]:
canvas = Canvas((250, 250))
In [11]:
sim.draw(canvas)
Out[11]:
In [12]:
#robot.setPose(4.5, 4.0, math.pi / 2)
In [13]:
robot.move(1, 1)
In [14]:
from IPython.display import display, clear_output
In [15]:
%%time
import time
for i in range(70):
sim.step(run_brain=False)
for r in sim.robots:
sim.draw(canvas)
clear_output(wait=True)
display(canvas)
time.sleep(.085) # sleep for a bit
CPU times: user 516 ms, sys: 24 ms, total: 540 ms
Wall time: 6.54 s
In [16]:
robot.getPose()
Out[16]:
(1.8312560941005205, 4.28695669992235, 2.287611019615305)
In [17]:
%%time
import numpy
light0 = numpy.zeros((100,100))
light1 = numpy.zeros((100,100))
robot.setPose(2.50, 4.50, math.pi / 2)
for i in range(70):
sim.step(run_brain=False)
for r in sim.robots:
x, y, a = robot.getPose()
light0[int(y/canvas.max_y * 100), int(x/canvas.max_x * 100)] = r.device["light"].scan[0]
light1[int(y/canvas.max_y * 100), int(x/canvas.max_x * 100)] = r.device["light"].scan[1]
CPU times: user 80 ms, sys: 0 ns, total: 80 ms
Wall time: 79.7 ms
70 steps * 0.1 seconds/step = 7 seconds
7 seconds / 85 ms
In [18]:
7 / .056
Out[18]:
125.0
Looks like it simulates about 80 - 100 simulated seconds for every real second, or is 80 - 100 times faster than real life.
Checking Light Readings¶
New simulated light sensors are 75% direct light and 25% ambient light.
In [19]:
%matplotlib inline
import matplotlib.pyplot as plt
In [20]:
fig1 = plt.figure()
sp0 = fig1.add_subplot(111)
p0 = sp0.matshow(light0, origin="lower")
fig1.colorbar(p0)
Out[20]:
<matplotlib.colorbar.Colorbar at 0x7fc1c4b5aba8>
In [21]:
fig2 = plt.figure()
sp1 = fig2.add_subplot(111)
p1 = sp1.matshow(light1, origin="lower")
fig2.colorbar(p1)
Out[21]:
<matplotlib.colorbar.Colorbar at 0x7fc1c4a64ef0>
Differences between two light sensors¶
In [22]:
fig3 = plt.figure()
sp3 = fig3.add_subplot(111)
p3 = sp3.matshow(light1 - light0, origin="lower")
fig3.colorbar(p3)
Out[22]:
<matplotlib.colorbar.Colorbar at 0x7fc1c49c1160>
In [23]:
def sampleLight(angle, resolution=50):
light0 = numpy.zeros((resolution,resolution))
light1 = numpy.zeros((resolution,resolution))
for x in range(resolution):
for y in range(resolution):
for r in sim.robots:
r.setPose(x/resolution * canvas.max_x,
y/resolution * canvas.max_y,
angle)
light0[y, x] = r.device["light"].scan[0]
light1[y, x] = r.device["light"].scan[1]
fig = plt.figure()
sp = fig.add_subplot(111)
p = sp.matshow(light0, origin="lower")
fig.colorbar(p)
In [24]:
robot.setPose(2.5, 4.5, 0)
sim.draw(canvas)
Out[24]:
In [25]:
sampleLight(0) # face up, north
In [26]:
robot.device["light"].scan
Out[26]:
[0.5453626300613212, 0.5139231911477004]
In [27]:
robot.setPose(2.5, 4.5, math.pi)
sim.draw(canvas)
Out[27]:
In [28]:
sampleLight(math.pi)
In [29]:
robot.setPose(2.5, 4.5, math.pi/2)
sim.draw(canvas)
Out[29]:
In [30]:
sampleLight(math.pi/2)
In [31]:
robot.setPose(2.5, 4.5, math.pi/4)
sim.draw(canvas)
Out[31]:
In [32]:
sampleLight(math.pi/4)
In [33]:
robot.setPose(2.5, 4.5, math.pi * 3/4)
sim.draw(canvas)
Out[33]:
In [34]:
sampleLight(math.pi * 3/4)
In [35]:
robot.setPose(2.5, 4.5, math.pi * 3/4)
sim.draw(canvas)
Out[35]:
In [36]:
robot.addDevice(Camera(120, 80))
Out[36]:
In [37]:
robot.device["camera"].getImage()
Out[37]:
In [38]:
robot.move(0, .5)
for i in range(10):
sim.step(run_brain=False)
In [39]:
robot.device["camera"].getImage()
Out[39]:
In [40]:
img = robot.device["camera"].getImage()
img = img.resize((240, 160))
img
Out[40]:
In [41]:
vsim = VSimulator(robot, world)
robot.brain = lambda self: self.move(1,1)